package com.gjp;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

@SpringBootApplication
@RestController
public class SpringbootEsApplication {

	@Autowired
	private TransportClient client;

	/**
	 * es查询
	 * @param id
	 * @return
	 */
	@GetMapping("/people/man/{id}")
	public ResponseEntity get(@PathVariable("id") String id){
        GetResponse response = client.prepareGet("people", "man", id).get();
        return new ResponseEntity(response.getSource(), HttpStatus.OK);
	}

	/**
	 * es增加
	 * @param name
	 * @param age
	 * @param country
	 * @param date
	 * @return
	 */
	@PostMapping("/people/man")
	public ResponseEntity add(@RequestParam("name") String name,
							  @RequestParam("age") Integer age,
							  @RequestParam("country") String country,
							  @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date){
		IndexResponse response = null;
		try {
			XContentBuilder source = XContentFactory.jsonBuilder()
					.startObject()
					.field("name", name)
					.field("age", age)
					.field("country", country)
					.field("date", date.getTime())
					.endObject();
			response = client.prepareIndex("people","man")
					//将文档json数据放入
					.setSource(source)
					.get();
		}catch (IOException e){
			e.printStackTrace();
			return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		//返回生成的ID
		return new ResponseEntity(response.getId(), HttpStatus.OK);
	}

	/**
	 * es删除
	 * @param id
	 * @return
	 */
	@DeleteMapping("/people/man/{id}")
	public ResponseEntity delete(@PathVariable("id") String id){
		DeleteResponse response = client.prepareDelete("people", "man", id).get();
		return new ResponseEntity(response.status(), HttpStatus.OK);
	}

	/**
	 * es更新
	 * @param id
	 * @return
	 */
	@PutMapping("/people/man")
	public ResponseEntity update(@RequestParam("id") String id,
								 @RequestParam(value = "name", required = false) String name,
								 @RequestParam(value = "age", required = false) Integer age,
								 @RequestParam(value = "country", required = false) String country,
								 @RequestParam(value = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date){
		UpdateResponse response = null;

		try {
			UpdateRequest updateRequest = new UpdateRequest("people","man",id);
			XContentBuilder source = XContentFactory.jsonBuilder()
					.startObject();
			if (name != null){
				source.field("name",name);
			}
			if (age != null){
				source.field("age",age);
			}
			if (country != null){
				source.field("country",country);
			}
			if (date != null){
				source.field("date",date);
			}
			source.endObject();
			//将更新文档放到更新请求中
			updateRequest.doc(source);

			//发送更新请求
			response = client.update(updateRequest).get();
		}catch (Exception e){
			e.printStackTrace();
			return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return new ResponseEntity(response.status(),HttpStatus.OK);
	}



	public static void main(String[] args) {
		SpringApplication.run(SpringbootEsApplication.class, args);
	}
}
